package com.salt.client.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.salt.common.json.AjaxJson;
import com.salt.modules.auth.api.CheckTokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class AccessFilter extends ZuulFilter {

	@Resource
	private CheckTokenService checkTokenService;

	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public int filterOrder() {
		return 0;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() {

		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		HttpServletResponse response = ctx.getResponse();
		response.setHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
		response.setHeader("Access-Control-Allow-Credentials","true");
		response.setHeader("Access-Control-Allow-Headers","x-requested-with, authorization, content-type");
		response.setHeader("Access-Control-Allow-Methods","POST,GET");
		response.setHeader("Access-Control-Expose-Headers","X-forwared-port, X-forwarded-host, Content-Disposition");
		response.setHeader("Vary","Origin,Access-Control-Request-Method,Access-Control-Request-Headers");
		String ua = request.getHeader("User-Agent");
		String token = request.getHeader("token");
		String url = request.getRequestURL().toString();

		/*if (StringUtils.isNotBlank(token)) {
			AjaxJson checkResult = checkTokenService.token(token);
			System.out.println("checkResult: " + checkResult);
		}*/

		if ((StringUtils.isNotBlank(ua) && StringUtils.isNotBlank(token)) ||
				url.contains("/sys/login") || url.contains("/sys/logout")||
				url.contains("/sys/refreshToken")||
				url.contains("/auth/login") || url.contains("/auth/logout")||
				url.contains("/auth/refreshToken")
				|| url.contains("/sys/sysConfig/queryById")
				|| url.contains("/app/sys/login") || url.contains("/sys/verify")
				|| url.contains("/sys/userRegister/register") || url.contains("/sys/user/login") || url.contains("/auth/user/login")
				// 积木
				|| url.contains("/api-tables") || url.contains("/jmreport")
				// 大屏
				/**
				/api-technology/technology/visual/realTime/process/flow/data
				/api-technology/technology/visual/energy/getEnergyTotal
				/api-technology/technology/visual/energy/getElec
				/api-technology/technology/visual/energy/getElec/detail
				/api-technology/technology/visual/energy/getWater
				/api-technology/technology/visual/energy/coal
				*/
				//|| url.contains("/technology/visual")
				/**
				 * /api-work/work/benchmarking/getRealTime
				 * /api-work/work/benchmarking/getMonth
				 */
				//|| url.contains("/work/benchmarking")
				/**
				 * /api-work/work/report/day?
				 */
				//|| url.contains("/work/report/day")
				/**
				 * /api-quality/quality/data/shift/statistical?
				 */
				//|| url.contains("/quality/data/shift/statistical")
				/**
				 * /api-quality/quality/handle/screen/list
				 * /api-quality/quality/handle/screen/num
				 */
				//|| url.contains("/quality/handle/screen")
				/**
				 * /api-quality/quality/jiaGongMaterial/factory?
				 */
				//|| url.contains("/quality/jiaGongMaterial/factory")
				/**
				 * /api-work/work/data/dayReport
				 * /api-work/work/data/getWorkData
				 */
				//|| url.contains("work/data")
				|| url.contains("api-onlinepreview")
				|| url.contains("api-pomp")
				|| url.contains("api-iot")
				|| url.contains("/openapi/monitor/data/login"))
				{
			//允许继续路由
			ctx.addZuulRequestHeader("token", token);
			ctx.addZuulRequestHeader("User-Agent", ua);
			ctx.setSendZuulResponse(true);
			ctx.setResponseStatusCode(200);
		}else{
			ctx.setSendZuulResponse(false);
			ctx.setResponseStatusCode(401);
		}
		return null;
	}




}
